package com.njuse_czf.kg.web.service.impl.handler;

import com.njuse_czf.kg.web.service.KGGraphService;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.text.similarity.LevenshteinDistance;

public abstract class AbstractQuestionHandler {
    private AbstractQuestionHandler nextHandler;

    /**
     * 设置nextHandler.
     *
     * @param nextHandler nextHandler
     */
    public void setNextHandler(AbstractQuestionHandler nextHandler) {
        this.nextHandler = nextHandler;
    }

    public AbstractQuestionHandler getNextHandler(){
        return this.nextHandler;
    }

    public abstract void handleQuestion(QuestionCtx questionCtx, String question, KGGraphService kgGraphService);

    public String fuzzyMatching(String name) {
        try {
            InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("matcher/fuzzy_matching_dict.txt");
            if(inputStream == null) {
                System.out.println("null");
            }
            InputStreamReader reader = new InputStreamReader(inputStream);
            BufferedReader buffReader = new BufferedReader(reader);
            String strTmp = "";

            while ((strTmp = buffReader.readLine()) != null) {
                String[] dict = strTmp.split(":");
                if (name.equals(dict[0])) {
                    return dict[1];
                }
            }
        }catch (IOException e){
            e.printStackTrace();
        }
        return name;
    }

        public List<String> getAllObject(String all){
        List<String> nodeNames = new ArrayList<String>();
        if(all.contains("and")){
            String[] names = all.split("and|,");
            for(int i = 0; i < names.length; i++){
                String str = names[i];
                if(str.charAt(0) == ' '){
                    str = str.substring(1);
                }
                int strLen = str.length();
                if(str.charAt(strLen-1) == ' '){
                    str = str.substring(0, strLen-1);
                }
                nodeNames.add(str);
            }
        }
        else{
            nodeNames.add(all);
        }
        return nodeNames;
    }

    public String getClosestNode(String name){
        LevenshteinDistance editDistance = new LevenshteinDistance();
        String path = "matcher/entity_set_lowercase.txt";
        List<String> entitys = new ArrayList<>();
        try{
            InputStream inputStream = getClass().getClassLoader().getResourceAsStream(path);
            InputStreamReader r = new InputStreamReader(inputStream);
            BufferedReader reader = new BufferedReader(r);
            String word;
            while((word = reader.readLine()) != null){
                entitys.add(word.trim());
            }
        }catch (IOException e){
            System.out.println("File is not available!");
        }
        String similarWord = "";
        int minDistance = Integer.MAX_VALUE;
        for(String entity : entitys){
            int distance = editDistance.apply(entity, name);
            if(minDistance > distance){
                similarWord = entity;
                minDistance = distance;
            }
        }
        return similarWord;
    }
}
